package cn.jaye.fabric.controller;

import cn.jaye.fabric.application.command.user.LoginUserCommand;
import cn.jaye.fabric.application.command.user.RegisterUserCommand;
import cn.jaye.fabric.application.UserApplicationService;
import cn.jaye.fabric.common.constants.exception.ExceptionEnum;
import cn.jaye.fabric.common.util.ResultUtil;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * Created by Administrator on 2018/5/7.
 */

@RestController
@RequestMapping("user")
public class UserController {

    @Autowired
    private UserApplicationService userApplicationService;

    @PostMapping("register")
    public String register(@RequestBody RegisterUserCommand registerUserCommand) throws Exception {
        String userPW = userApplicationService.register(registerUserCommand);
        if (StringUtils.isNotBlank(userPW)) {
            return ResultUtil.success(userPW);
        }
        return ResultUtil.error(ExceptionEnum.USER_REGISTER_FAILED);
    }

    @PostMapping("login")
    public String login(@RequestBody LoginUserCommand loginUserCommand) throws Exception {
        return ResultUtil.success(userApplicationService.login(loginUserCommand));
    }
}
